// pages/teacherName1/teacherName1.js
var api = require('../../utils/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    class_hour:[],
    houridx:0,
    array: [1, 2, 3],
    hidden: true,
    typeList: [
      { type: 0, isActive: 0, name: '迟到' },
      { type: 1, isActive: 0, name: '早退' },
      { type: 2, isActive: 0, name: '请假' },
      { type: 3, isActive: 0, name: '旷课' }
    ]
  },


  cancel: function () {
    this.setData({
      hidden: true
    });
  },

  //统计迟到、旷课人数
  getKaoqinNum(){
    var studentList = this.data.studentList;
    var late = [];
    var leave = [];
    var qing = [];
    var kuang = [];
    var nomal = studentList.filter(item => {
      return item.status == 1
    })

    studentList.forEach((item, index, arr) => {

      item.typeList.forEach(item1 => {
        if (item1.isActive == 1 && item1.name == "迟到") {
          late.push(item)
        } else if (item1.isActive == 1 && item1.name == "早退") {
          leave.push(item)
        } else if (item1.isActive == 1 && item1.name == "请假") {
          qing.push(item)
        } else if (item1.isActive == 1 && item1.name == "旷课") {
          kuang.push(item)
        }

      })


    })
    console.log(leave)
    this.setData({
      late,
      leave,
      qing,
      kuang,
      nomal,
      studentList,

    })
  },

  //下课考勤提交
  confirm: function () {
    var studentList = this.data.studentList;
    var info=[];
    var student_info=[];
    this.data.studentList.forEach(item=>{
      var type='';
      var status;
      item.status == 2 || item.status == 3 ? status = 3 : status = 1

      if (item.typeList.some(item1 => { return item1.isActive == 1 && item1.name == "迟到"})){
        type = 0
      }
      if (item.typeList.some(item1 => { return item1.isActive == 1 && item1.name == "早退" })) {
        type = 3
      }
      if (item.typeList.some(item1 => { return item1.isActive == 1 && item1.name == "请假" })) {
        type = 2
      }
      if (item.typeList.some(item1 => { return item1.isActive == 1 && item1.name == "旷课" })) {
        type = 1
      }
      if ((item.status == 2 || item.status == 3) && item.typeList.every(item1 => { return item1.isActive == 0})) {
        type = 4
      }

      if (item.status == 1) {
        type = 5
      }

      info.push([item.id, type, status]);
      student_info.push(item.student_info_id)

      // console.log(this.data.studentList)
    })
    var attend_classArr = studentList.filter(item=>{
      // return item.status == 3 || item.status == 2
      return item.typeList[2].isActive == 0 && item.typeList[3].isActive == 0 && item.status != 1
    })

    var params={
      token: wx.getStorageSync('token'),
      campus:this.data.campus,
      teacher_id: this.data.teacher_id,
      should_arrive:this.data.studentList.length,
      attend_class: attend_classArr.length,
      leave: this.data.qing.length,
      late: this.data.late.length,
      truancy: this.data.kuang.length,
      leave_early: this.data.leave.length,
      placement_id: this.data.placement_id,
      roll_call_time: this.data.roll_call_time,
      info,
      student_info,
      campus:this.data.campus,
      teacher_att_id:this.data.teacher_att_id,
      class_hour:this.data.class_hour[this.data.houridx]
    }

  
    api.fetchApi('teacher/submit_to', params).then(res => {
      api.showToast('提交成功').then(() => {
        
        //清除点名时退出状态
        wx.setStorageSync('isDian', 0)

        setTimeout(() => {
          // wx.navigateTo({
          //   url: '../teacherKaoqinInfor/teacherKaoqinInfor',
          // })
          wx.reLaunch({
            url: '../personal/personal',
          })
        }, 1500)
      })

    })

  },

  subimitInfor() {
    this.setData({
      hidden: false
    });

    this.getKaoqinNum()
  },

//选择课时
  hourPicke(e){
    console.log(e.detail.value);
    this.setData({
      houridx: e.detail.value
    })
  },
  // //点名
  rollCall(e) {
    

    var student_id = e.currentTarget.dataset.id;
    console.log(student_id)
    var studentList = this.data.studentList;
    console.log(studentList)
    studentList.forEach(item => {
      if (student_id == item.student_id) {
        if (item.status == 1) {
          item.status = 3
           item.mark = 1
        } else {
          item.status = 1
          item.mark = ''
        }

      }

      if (item.status == 1) {
        item.typeList.forEach(item1 => {
          item1.isActive = 0
        })
      }

    })

    this.setData({
      studentList
    })

  
  },

  //选择点击考勤
  handleKaoqin(e) {

    var typeList = this.data.typeList;
    var num = e.currentTarget.dataset.num
    var type = e.currentTarget.dataset.type;
    var studentList = this.data.studentList;
    studentList[num].typeList.forEach((item, index, arr) => {

      if (item.type == type) {
        if (item.isActive == 0) {

          arr.forEach(item1 => {
            item1.isActive = 0
          })
          item.isActive = 1

        } else {
          item.isActive = 0
          if (!arr.every(item1 => {
            return item1.isActive = 0
          })) {
            studentList[num].status = 1
          }
        }
      }

    })


    this.setData({
      // type,
      // typeList,
      // num,
      studentList
    })



    studentList.forEach((item, index, arr) => {

      item.typeList.forEach(item1 => {
        if (item1.isActive == 1 && item1.name == "迟到") {
          item.status = 3
          item.mark = 1 
        } else if (item1.isActive == 1 && item1.name == "早退") {
          item.status = 3
          item.mark = 1 
        } else if (item1.isActive == 1 && item1.name == "请假") {
           item.status = 3   
          item.mark = ''
        } else if (item1.isActive == 1 && item1.name == "旷课") {
           item.status = 3
          item.mark = ''
        }
        //  else if (item.typeList.every(item2 => { return item2.isActive == 0})) {
        //   nomal.push(item)
        // } 

      })


    })

    this.setData({

      studentList

    })

  

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var params = JSON.parse(options.studentParams)
    console.log(options)
    var course_id = params.course_id
    console.log(course_id)
    api.fetchApi('teacher/school_attendance', params).then(res => {

      res.data.forEach((item, index, arr) => {
        item.typeList = [
          { type: index + "" + 0, isActive: 0, name: '迟到' },
          { type: index + "" + 1, isActive: 0, name: '早退' },
          { type: index + "" + 2, isActive: 0, name: '请假' },
          { type: index + "" + 3, isActive: 0, name: '旷课' }
        ]
      })
      var teacher_att_id = res.teacher_att_id;
      //wx.setStorageSync('teacher_att_id', teacher_att_id);
      console.log(res.data)
     
      var teacher_id = res.data[0].teacher_id;
      var roll_call_time = res.roll_call_time;
      var placement_id = res.data[0].placement;
      var campus = res.data[0].campus_id;
      var class_hour = res.class_hour;
      console.log(roll_call_time)
      this.setData({
        studentList: res.data,
        class1: options.class1,
        project: options.project,
        school: options.school,
        teacher_id,
        roll_call_time,
        placement_id,
        campus,
        teacher_att_id,
        class_hour,
        course_id
      })
      console.log(this.data.studentList)

    })


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})